package development;

import java.util.Vector;

import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.component.Event;
import state.component.EventListener;
import state.component.MultiTab;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import development.bean.UserBean;
import development.group.FrmGroupListTableFreeUserChat;
import development.group.FrmGroupMenu;
import development.group.FrmGroupTableFreeUserFriend;
import development.socket.SocketClientUtil;

public class FrmListFreeUser extends GameForm implements EventListener {
  private final int DISTANCE = 21;
  private final int DX = 10;
  private final int DY = 48;
  
  private GameImage checkedImage;
  private GameImage uncheckedImage;
  private GameImage popupImage;
  
  private ImageText text8;
  
  private int chooseIndex;
  public int pagenummber;
  public boolean isReceivedData;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmListFreeUser();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    isReceivedData = false;
    Global.freeUser.removeAllElements();
    SocketClientUtil.getUserWantToPlayList(0);
    chooseIndex = 0;
    popupImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "bg_popup.png");
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    checkedImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "Tickbox_1.png");
    uncheckedImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "Tickbox_2.png");
    DrawUtils.setBottomBar(_manager, null, null, "Trở về");
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    if (parentGroup == null) {
      DrawUtils.drawTopBar(g);
      text8.drawString2(g, "Danh sách mời chơi", GameColor.WHITE_CODE, Constants.SCREEN_WIDTH / 2, 2, Constants.TOP_HCENTER_ANCHOR);
    }
    
    if (!isReceivedData) {
      g.drawImage(popupImage, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
      text8.drawString2(g, "Đang cập nhật danh sách", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
    } else if (isReceivedData && (Global.freeUser.size() == 0)) {
      g.drawImage(popupImage, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
      text8.drawString2(g, "Không có người chơi đang chờ", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
    } else {
      for (int i = 0; i < Global.freeUser.size(); i++) {
        g.setColor(GameColor.WHITE_CODE);
        g.drawLine(DX, DY - DISTANCE / 2 + i * DISTANCE, GameConstants.SCREEN_WIDTH - DX, DY - DISTANCE / 2 + i * DISTANCE);
        
        UserBean user = (UserBean) Global.freeUser.elementAt(i);
        int color = GameColor.WHITE_CODE;
        if (i == chooseIndex) {
          g.setColor(0x662221);
          g.fillRect(DX, DY - DISTANCE / 2 + i * DISTANCE + 2, GameConstants.SCREEN_WIDTH - 2 * DX, DISTANCE - 3);
          color = GameColor.YELLOW_CODE;
          
          if (user.isChoosed()) {
            DrawUtils.getBottomBar(_manager).setCenterLabel("Bỏ chọn");
          } else {
            DrawUtils.getBottomBar(_manager).setCenterLabel("Chọn");
          }
        }
        
        if (user.isChoosed()) {
          g.drawImage(checkedImage, 15, DY + i * DISTANCE, GameConstants.CENTER_ANCHOR);
        } else {
          g.drawImage(uncheckedImage, 15, DY + i * DISTANCE, GameConstants.CENTER_ANCHOR);
        }
        
        text8.drawString2(g, user.getUsername(), color, 26, DY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
        text8.drawString2(g, user.getLocationInfo(), color, GameConstants.SCREEN_WIDTH / 2 - 30, DY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
      }

      if (Global.freeUser.size() > 0) {
        DrawUtils.getBottomBar(_manager).setLeftLabel("Mời");
      }
    }
  }
  
  public void onActionPerform(Event event) {
    if (MultiTab.OPEN_TAB_EVENT.equals(event.getAction())) {
      DrawUtils.setBottomBar(_manager, null, null, "Trở về");
      Global.freeUser.removeAllElements();
      chooseIndex = 0;
      SocketClientUtil.getUserWantToPlayList(0);
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.LEFT:
      if (parentGroup != null && parentGroup instanceof FrmGroupListTableFreeUserChat) {
        FrmGroupListTableFreeUserChat.setTab(0);
      }
      break;
    case Key.RIGHT:
      if (parentGroup != null && parentGroup instanceof FrmGroupListTableFreeUserChat) {
        FrmGroupListTableFreeUserChat.setTab(2);
      }
      break;
    case Key.UP:
    case Key.K_2:
      chooseIndex--;
      if (chooseIndex < 0) {
        chooseIndex = Global.freeUser.size() - 1;
      }
      break;
    case Key.DOWN:
    case Key.K_8:
      chooseIndex++;
      if (chooseIndex > Global.freeUser.size() - 1) {
        chooseIndex = 0;
      }
      break;
    case Key.FIRE:
    case Key.K_5:
      if (Global.freeUser.size() > 0) {
        UserBean user = (UserBean) Global.freeUser.elementAt(chooseIndex);
        user.setChoosed(!user.isChoosed());
      }
      break;
    case Key.SOFT_RIGHT:
      if (parentGroup != null) {
        if (parentGroup instanceof FrmGroupListTableFreeUserChat) {
          SocketClientUtil.leaveRoomRequest();
          GameGlobal.nextState(FrmListRoom.instance(), null);
        } else if (parentGroup instanceof FrmGroupTableFreeUserFriend) {
          FrmGroupTableFreeUserFriend.setTab(0);
        }
      } else {
        if (Global.systemCanvas.lastState instanceof FrmDownloadResource) {
          GameGlobal.nextState(FrmGroupMenu.instance(), null);
        } else {
          GameGlobal.nextState(Global.systemCanvas.lastState, null);
        }
      }
      break;
    case Key.SOFT_LEFT:
      if (Global.freeUser.size() > 0) {
        Vector choosedUsers = new Vector();
        for (int i = 0; i < Global.freeUser.size(); i++) {
          UserBean choosedUser = (UserBean) Global.freeUser.elementAt(i);
          if (choosedUser.isChoosed()) {
            choosedUsers.addElement(choosedUser.getUsername());
          }
        }
        
        if (choosedUsers.size() == 0) {
          Global.alert.showAlert(this, GameAlert.OK_TYPE, "Hãy chọn người chơi");
        } else {
          SocketClientUtil.invitePlay(choosedUsers, Global.currentGame.getId());
          Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đã gửi lời mời", 99).setAlertTimeOut(1000);
        }
      }
      break;
    }
  }
  
  public void alertEventPerform(int alertType, int eventType, int alertId) {
    if (alertId == 99) {
      if (parentGroup instanceof FrmGroupTableFreeUserFriend) {
        FrmGroupTableFreeUserFriend.setTab(0);
        return;
      }
      GameGlobal.nextState(Global.systemCanvas.lastState, null);
    }
  }

  public void destroy() {
    text8 = null;
    checkedImage = null;
    uncheckedImage = null;
    popupImage = null;
  }
}
